package cc.dowell.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cc.dowell.enums.HttpCodeEnum;
import cc.dowell.pojo.Zone;
import cc.dowell.service.ZoneService;
import cc.dowell.utils.R;
import cc.dowell.vo.ZoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/zone")
public class ZoneController {

    @Autowired
    private ZoneService zoneService;

    @GetMapping("/{pageNum}/{pageSize}")
    public R<?> listZone(@PathVariable long pageNum,@PathVariable long pageSize) {
        Page<Zone> zonePage = new Page<>(pageNum, pageSize);
        IPage<ZoneVo> zoneVoPage = zoneService.selectZonePage(zonePage);

        return R.success(zoneVoPage);
    }

    @GetMapping("/{id}")
    public R<?> zoneInfo(@PathVariable("id") Long id){
        ZoneVo zoneVo = zoneService.zoneInfo(id);
        if(zoneVo!=null){
            return R.success(zoneVo);
        }
        return R.error();
    }

    @PostMapping()
    public R<?> addZone(@RequestBody Zone zone) {
        Boolean b = zoneService.addZone(zone);
        if(b){
            return R.success(HttpCodeEnum.SUCCESS);
        }
        return R.error();
    }

    @DeleteMapping("/{id}")
    public R<?> deleteZone(@PathVariable("id") Long id) {
        Boolean b = zoneService.deleteZone(id);
        if(b){
            return R.success(HttpCodeEnum.SUCCESS);
        }
        return R.error();
    }

    @PutMapping
    public R<?> updateZone(@RequestBody Zone zone) {
        Boolean b = zoneService.updateZone(zone);
        if(b){
            return R.success(HttpCodeEnum.SUCCESS);
        }
        return R.error();
    }


}